<?
require_once('./init.php');
if($_GET["do"]=="loadinfo")
{
	$q=query("select code,vote,vote1,vote2 from baithi order by code asc");
	while($r=fetch($q))
	{
		$arr[]=$r;
	}
	echo json_encode($arr);
	exit;
}
if($_GET["do"]=="cronjob"){
	$q=query("select * from baithi");
	while ($r=fetch($q)) {
		# code...
		if($r["vote2"]<=0) continue;
		$rand=0;
		if($r["vote2"]<10) $rand=$r["vote2"];
		else $rand = rand(1,10);
		query("update baithi set vote1=vote1+$rand,vote2=vote2-$rand where code='$r[code]'");
	}
	exit;
}
if($_GET["do"]=="update"){
	$inc=intval($_GET['inc']);
	$code=$_GET['code'];
	$q=query("update baithi set vote2=vote2+$inc where code='$code'");
	exit;
}
if($_GET["do"]=="logs"){
	$inc=intval($_GET['inc']);
	$code=$_GET['code'];
	$time=time();
	query("insert into fakevote (code,vote,date) VALUES('$code',$inc,$time)");
	exit;
}
?>
<style>
.list-baithi{
	margin: 0px;
	padding: 0px;
}
.list-baithi li{
}
.list-baithi li span{
	border-right: 1px solid #fff;
}
.li1{
	background:#fbf7ec;
}
.li0{
	background: #eceae8;
}
.code{
	width: 50px;
	display: inline-block;
}
.name{
	width: 200px;
	display: inline-block;
}
.vote{
	width: 100px;
	display: inline-block;
}
.tcat{
	background: #4f4f4f;
	color: #fff;
position: relative;
}
.hover{
	background: #cbcdce;
	-webkit-transition: 0ms ease-in-out;
    -moz-transition: 0ms ease-in-out;
    -o-transition: 0ms ease-in-out;
    transition: 0ms ease-in-out;
}
.susscess{
	background: #e3ffcc !important;
	-webkit-transition: 500ms ease-in-out;
    -moz-transition: 500ms ease-in-out;
    -o-transition: 500ms ease-in-out;
    transition: 500ms ease-in-out;
}
.value{
	-webkit-transition: 500ms ease-in-out;
    -moz-transition: 500ms ease-in-out;
    -o-transition: 500ms ease-in-out;
    transition: 500ms ease-in-out;
}
.sumvote, .sumvote2, .sumvotecat{
	width: 140px;
}
</style>
<meta charset='utf-8'>
<script type="text/javascript" src='./js/jquery.min1.7.2.js'></script>
<center><b>Thống kê Vote Oscar For Hair 2014</b></center>
<?
$q=query("select * from baithi order by code asc");
echo "<ul class='list-baithi'><li class='tcat'>
		<span class='code'>Mã Số</span>
		<span class='name'>Họ Tên</span>
		<span class='vote'>Vote Thật</span>
		<span class='vote'>Vote Tăng</span>
		<span class='vote'>Vote Sẽ Tăng</span>
		<span class='vote sumvotecat'>Tổng Vote hiện tại</span>
		<span class='vote sumvotecat'>Tổng Vote sau tăng</span>
	</li>";
$i=0;
while($r=fetch($q)){
	$i=($i+1)%2;
	echo "<li class='li$i value' id='li$r[code]'>
		<span class='code'>$r[code]</span>
		<span class='name'>$r[hoten]</span>
		<span class='vote votethat'>$r[vote]</span>
		<span class='vote incvote'>$r[vote1]</span>
		<span class='vote vote2'>$r[vote2]</span>
		<span class='vote sumvote'>".($r['vote']+$r['vote1'])."</span>
		<span class='vote sumvote2'>".($r['vote']+$r['vote1']+ $r["vote2"])."</span>
		
	</li>";
}
?>
<li class='tcat' style="text-align:center;height:25px;padding:5px;">
<span style='float:left;border:none;'>Load after: <span id='time' style='border:none;'></span>s</span>
	</li>
</ul>
<script type="text/javascript">
function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
}
	$("li.value").hover(function(){
		$(this).addClass("hover");
	},
	function(){
		$(this).removeClass("hover");
	});

	function update(){
		$(".list-baithi li.value").each(function(){
			var incval = parseInt($(this).find(".newvote").val());
			var code= $(this).find(".code").html();
			var li=$(this);
			if(!isNaN(incval) && incval!=0){
				$.get("./changevote.php?do=update&code="+code+"&inc="+incval,function(data){
						loadinfo();
				});
			}
		});
	}
	function loadinfo(){
			$.get("./changevote.php?do=loadinfo",function(data){
				$.each(data,function(i,data){
					var li= $("#li"+data["code"]);
					li.find(".votethat").html(data["vote"]);
					li.find(".incvote").html(data["vote1"]);
					li.find(".vote2").html(data["vote2"]);
					li.find(".sumvote").html(parseInt(data["vote"])+parseInt(data["vote1"]));
					li.find(".sumvote").html(parseInt(data["vote"])+parseInt(data["vote1"])+parseInt(data["vote2"]));
					li.addClass("susscess");
						setTimeout(function(){
							li.removeClass("susscess");
						},1500+i*50);	
				});
			},"JSON");
	}
	function log(){
		$(".list-baithi li.value").each(function(){
			var incval = parseInt($(this).find(".newvote").val());
			var code= $(this).find(".code").html();
			if(!isNaN(incval) && incval!=0){
				$.get("./changevote.php?do=logs&code="+code+"&inc="+incval);
			}
		});
	}
	$("#update").click(function(){
		log();
		update();
	});
	var time=0;
	function clock(max){
		time++;
		var con= max-time;
		$("#time").html(con);
		if(max==time) time=0;
	}
	setInterval(function(){loadinfo();},60000);
	setInterval(function(){clock(60)},1000);
</script>